package com.uroad.lib.widget.photopicker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;

import java.util.ArrayList;

public class PhotoPickUtils {

    public static Bitmap headPic = null;

    public static void onActivityResult(int requestCode, int resultCode, Intent data, PickHandler pickHandler) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PhotoPicker.REQUEST_CODE) {//第一次，选择图片后返回
                if (data != null) {
                    ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                    pickHandler.onPickSuccess(photos);
                } else {
                    pickHandler.onPickFail("选择图片失败");
                }
            } else if (requestCode == PhotoPreview.REQUEST_CODE) {//如果是预览与删除后返回
                if (data != null) {
                    ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                    pickHandler.onPreviewBack(photos);
                } else {
                    // pickHandler.onPickFail("选择图片失败");
                }
            }
        } else {
            if (requestCode == PhotoPicker.REQUEST_CODE) {
                pickHandler.onPickCancle();
            }
        }
    }

    public static void startPick(Activity context, ArrayList<String> photos, int size) {
        PhotoPicker.builder()
                .setPhotoCount(size)
                .setShowCamera(true)
                .setShowGif(false)
                .setSelected(photos)
                .setPreviewEnabled(true)
                .start(context, PhotoPicker.REQUEST_CODE);
    }

    /**
     * 选择头像
     * @param context
     * @param photos
     * @param size
     */
    public static void startPickByHeadPic(Activity context, ArrayList<String> photos, int size) {
        PhotoPicker.builder()
                .setPhotoCount(size)
                .setShowCamera(true)
                .setShowGif(false)
                .setSelected(photos)
                .setPreviewEnabled(true)
                .setHeadpic(true)
                .start(context, PhotoPicker.REQUEST_CODE);
    }

    public static void openCamera(){}


    public interface PickHandler {
        void onPickSuccess(ArrayList<String> photos);

        void onPreviewBack(ArrayList<String> photos);

        void onPickFail(String error);

        void onPickCancle();
    }
}
